/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmCustomer.java
 *
 * Created on Mar 15, 2011, 10:46:15 PM
 */
package inventorymanagement;

import model.ColorTable;
import DataAccess.TableAccess;
import Entity.Customer;
import interact.iCheckForm;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 *
 * @author Home
 */
public class frmCustomer extends javax.swing.JFrame {

    /** Creates new form frmCustomer */
    public frmCustomer() {
        initComponents();
        this.setLocation();
        try {
            //TableAccess.readToTable("select * from Customers", tblCustomer);
            TableAccess.readToTableCus("select * from Customers", tblCustomer);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //refresh();
    }

    private void setLocation() {
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (int) ((d.getWidth() - getWidth()) / 2);
        int y = (int) ((d.getHeight() - getHeight()) / 2);
        setLocation(x, y);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblCustomer = new ColorTable();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        txtCustomerID = new javax.swing.JTextField();
        txtCustomerName = new javax.swing.JTextField();
        txtCustomerPhone = new javax.swing.JTextField();
        txtCustomerEmail = new javax.swing.JTextField();
        txtCustomerAddress = new javax.swing.JTextField();
        rdoMale = new javax.swing.JRadioButton();
        rdoFeMale = new javax.swing.JRadioButton();
        btnAddCustomer = new javax.swing.JButton();
        btnRefesh = new javax.swing.JButton();
        btnEdit = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        txtSearchCustomerID = new javax.swing.JTextField();
        txtSearchCustomerName = new javax.swing.JTextField();
        btnSearchRefresh = new javax.swing.JButton();
        jLabel10 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Customer Management");

        tblCustomer.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tblCustomer.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblCustomerMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblCustomer);

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder(new java.awt.Color(51, 153, 255), null));

        jLabel1.setText("ID:");

        jLabel2.setText("Name:");

        jLabel3.setText("Phone:");

        jLabel4.setText("Email:");

        jLabel5.setText("Address:");

        jLabel6.setText("Gender:");

        txtCustomerID.setEditable(false);

        buttonGroup1.add(rdoMale);
        rdoMale.setText("Male");

        buttonGroup1.add(rdoFeMale);
        rdoFeMale.setText("Female");

        btnAddCustomer.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Add.png"))); // NOI18N
        btnAddCustomer.setText("Add");
        btnAddCustomer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddCustomerActionPerformed(evt);
            }
        });

        btnRefesh.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Symbol-Refresh.png"))); // NOI18N
        btnRefesh.setText("Refesh");
        btnRefesh.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRefeshActionPerformed(evt);
            }
        });

        btnEdit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/edit.png"))); // NOI18N
        btnEdit.setText("Edit");
        btnEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditActionPerformed(evt);
            }
        });

        jLabel7.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/customer-11.jpg"))); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(jLabel1)
                                        .addGap(16, 16, 16))
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel3))
                                .addGap(18, 18, 18)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txtCustomerPhone, javax.swing.GroupLayout.DEFAULT_SIZE, 219, Short.MAX_VALUE)
                                    .addComponent(txtCustomerName, javax.swing.GroupLayout.DEFAULT_SIZE, 219, Short.MAX_VALUE)
                                    .addComponent(txtCustomerID, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 219, Short.MAX_VALUE)))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel6)
                                .addGap(18, 18, 18)
                                .addComponent(rdoMale)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(rdoFeMale))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel5)
                                    .addComponent(jLabel4))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txtCustomerEmail, javax.swing.GroupLayout.DEFAULT_SIZE, 218, Short.MAX_VALUE)
                                    .addComponent(txtCustomerAddress, javax.swing.GroupLayout.DEFAULT_SIZE, 218, Short.MAX_VALUE))))
                        .addGap(72, 72, 72))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(19, 19, 19)
                        .addComponent(btnAddCustomer, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnEdit, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnRefesh)
                        .addGap(77, 77, 77)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(47, 47, 47))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtCustomerID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtCustomerName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtCustomerPhone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtCustomerEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(txtCustomerAddress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(rdoMale)
                    .addComponent(rdoFeMale))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAddCustomer)
                    .addComponent(btnEdit)
                    .addComponent(btnRefesh))
                .addGap(79, 79, 79))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(37, 37, 37)
                .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 164, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(112, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Manager", jPanel1);

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder(new java.awt.Color(51, 153, 255), null));

        jLabel8.setText("Customer ID:");

        jLabel9.setText("Customer Name:");

        txtSearchCustomerID.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtSearchCustomerIDKeyReleased(evt);
            }
        });

        txtSearchCustomerName.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtSearchCustomerNameKeyReleased(evt);
            }
        });

        btnSearchRefresh.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Symbol-Refresh.png"))); // NOI18N
        btnSearchRefresh.setText("Refresh");
        btnSearchRefresh.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchRefreshActionPerformed(evt);
            }
        });

        jLabel10.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/searchCustomer.jpg"))); // NOI18N
        jLabel10.setText("jLabel10");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel8)
                            .addComponent(jLabel9))
                        .addGap(28, 28, 28)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtSearchCustomerName)
                            .addComponent(txtSearchCustomerID, javax.swing.GroupLayout.DEFAULT_SIZE, 143, Short.MAX_VALUE)))
                    .addComponent(btnSearchRefresh))
                .addGap(18, 18, 18)
                .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 249, Short.MAX_VALUE)
                .addGap(27, 27, 27))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(22, 22, 22)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel8)
                            .addComponent(txtSearchCustomerID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(26, 26, 26)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel9)
                            .addComponent(txtSearchCustomerName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(24, 24, 24)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 191, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnSearchRefresh))))
                .addContainerGap(28, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Search", jPanel2);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 564, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 564, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 134, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnAddCustomerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddCustomerActionPerformed
        // TODO add your handling code here:
        boolean succ;
        if (!check()) {
            return;
        } else {
            try {
                Customer cus = new Customer();
                cus.setCustomerName(txtCustomerName.getText());
                cus.setEmail(txtCustomerEmail.getText());
                cus.setAddress(txtCustomerAddress.getText());
                cus.setPhone(txtCustomerPhone.getText());
                cus.setPurchased(0);
//                int i = cboStatus.getSelectedIndex();
//                //System.out.println(i);
//                if (i == 0) {
//                    cus.setStatus(true);
//                } else {
//                    cus.setStatus(false);
//                }

                if (rdoMale.isSelected()) {
                    cus.setGender(true);
                } else {
                    cus.setGender(false);
                }

                succ = DataAccess.Customer.insertCustomer(cus);
                if (!succ) {
                    JOptionPane.showMessageDialog(this, "Customer name is not Add", "ERROR", JOptionPane.ERROR_MESSAGE);
                } else {
                    JOptionPane.showMessageDialog(this, "Customer name is Added Successfull", "Infomation", JOptionPane.INFORMATION_MESSAGE);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            this.refresh();
            this.resetForm();
        }
    }//GEN-LAST:event_btnAddCustomerActionPerformed

    private void btnRefeshActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRefeshActionPerformed
        // TODO add your handling code here:
        this.refresh();
        this.resetForm();
    }//GEN-LAST:event_btnRefeshActionPerformed

    private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditActionPerformed
        // TODO add your handling code here:
        boolean succ;
        if (!check()) {
            return;
        } else {
            try {
                Customer cus = new Customer();
                cus.setCustomerID(Integer.parseInt(txtCustomerID.getText()));
                cus.setCustomerName(txtCustomerName.getText());
                cus.setEmail(txtCustomerEmail.getText());
                cus.setAddress(txtCustomerAddress.getText());
                cus.setPhone(txtCustomerPhone.getText());
                cus.setPurchased(0);
//                int i = cboStatus.getSelectedIndex();
//                if (i == 0) {
//                    cus.setStatus(true);
//                } else {
//                    cus.setStatus(false);
//                }
                if (rdoMale.isSelected()) {
                    cus.setGender(true);
                } else {
                    cus.setGender(false);
                }

                succ = DataAccess.Customer.updateCustomer(cus);
                if (!succ) {
                    JOptionPane.showMessageDialog(this, "Customer is not Edit", "ERROR", JOptionPane.ERROR_MESSAGE);
                } else {
                    JOptionPane.showMessageDialog(this, "Customer is Edit Successfull", "Infomation", JOptionPane.INFORMATION_MESSAGE);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            this.refresh();
            this.resetForm();
        }
    }//GEN-LAST:event_btnEditActionPerformed


    public void deleteCus()
    {
//        boolean succ;
//        try {
//            int i = Integer.parseInt(txtCustomerID.getText());
//            succ = DataAccess.Customer.deleteCustomer(i);
//            if (!succ) {
//                JOptionPane.showMessageDialog(this, "Customer is not Delete", "ERROR", JOptionPane.ERROR_MESSAGE);
//            } else {
//                JOptionPane.showMessageDialog(this, "Customer is Delete Successfull", "Infomation", JOptionPane.INFORMATION_MESSAGE);
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
//        this.refresh();
//        this.resetForm();
    }
    private void tblCustomerMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblCustomerMouseClicked
        // TODO add your handling code here:
        try {
            int i = tblCustomer.getSelectedRow();
            txtCustomerID.setText(String.valueOf(tblCustomer.getValueAt(i, 0)));
            txtCustomerName.setText(String.valueOf(tblCustomer.getValueAt(i, 1)));
            txtCustomerAddress.setText(String.valueOf(tblCustomer.getValueAt(i, 2)));
            txtCustomerPhone.setText(String.valueOf(tblCustomer.getValueAt(i, 3)));
            txtCustomerEmail.setText(String.valueOf(tblCustomer.getValueAt(i, 4)));
            Object gender = tblCustomer.getValueAt(i, 5);

            if (gender.toString().equals("Male")) {
                rdoMale.setSelected(true);
            } else {
                rdoFeMale.setSelected(true);
            }
            
//        System.out.println(Boolean.parseBoolean(gender.toString()));
//        System.out.println(gender.toString());
//            if (Integer.parseInt(gender.toString()) == 1) {
//                rdoMale.setSelected(true);
//            } else {
//                rdoFeMale.setSelected(true);
//            }
//            Object active = tblCustomer.getValueAt(i, 6);
//            System.out.println(active);
//            if (Integer.parseInt(active.toString()) == 1) {
//                cboStatus.setSelectedIndex(0);
//            } else {
//                cboStatus.setSelectedIndex(1);
//            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }//GEN-LAST:event_tblCustomerMouseClicked

    private void txtSearchCustomerIDKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtSearchCustomerIDKeyReleased
        // TODO add your handling code here:
        String str = txtSearchCustomerID.getText();
        String sql = "";
        if (str.equals("")) {
            sql = "select * from Customers";
        }
        else if(iCheckForm.checkNumber(str)) {
            JOptionPane.showMessageDialog(this, "Customer ID is not validated", "ERROR", JOptionPane.ERROR_MESSAGE);
            txtSearchCustomerID.setText("");
            return;
        }
        else {
            sql = ("select * from Customers where CustomerID=" + Integer.parseInt(str));
        }
        try {
            TableAccess.readToTableCus(sql, tblCustomer);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }//GEN-LAST:event_txtSearchCustomerIDKeyReleased

    private void txtSearchCustomerNameKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtSearchCustomerNameKeyReleased
        // TODO add your handling code here:
        String str = txtSearchCustomerName.getText();
        String sql = "";
        if (str.equals("")) {
            sql = "select * from Customers";
        } else {
            sql = ("select * from Customers where CustomerName like '%" + str + "%'");
        }
        try {
            TableAccess.readToTableCus(sql, tblCustomer);
        } catch (Exception e) {
            e.printStackTrace();
        }

        //        if (!isNumberic(str)) {
//            JOptionPane.showMessageDialog(this, "Customer ID is not validated", "ERROR", JOptionPane.ERROR_MESSAGE);
//            txtSearchCustomerID.requestFocus();
//        }

    }//GEN-LAST:event_txtSearchCustomerNameKeyReleased

    private void btnSearchRefreshActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchRefreshActionPerformed
        // TODO add your handling code here:
        this.refresh();
        this.resetForm();
    }//GEN-LAST:event_btnSearchRefreshActionPerformed

    private void refresh() {
        try {
            TableAccess.readToTableCus("select * from Customers", tblCustomer);
        } catch (Exception e) {
        }
    }

    private boolean check() {
        if (txtCustomerName.getText().trim().length() == 0) {
            JOptionPane.showMessageDialog(this, "Customer name is not validated", "ERROR", JOptionPane.ERROR_MESSAGE);
            txtCustomerName.requestFocus();
            return false;
        }
        if (txtCustomerPhone.getText().trim().length() == 0) {
            JOptionPane.showMessageDialog(this, "Phone is not validated", "ERROR", JOptionPane.ERROR_MESSAGE);
            txtCustomerPhone.requestFocus();
            return false;
        }
        if(iCheckForm.checkNumber(txtCustomerPhone.getText()))
        {
            JOptionPane.showMessageDialog(this, "Phone is not validated", "ERROR", JOptionPane.ERROR_MESSAGE);
            txtCustomerPhone.requestFocus();
            return false;
        }
        if (!checkEmail(txtCustomerEmail.getText())) {
            JOptionPane.showMessageDialog(this, "Email is not validated", "ERROR", JOptionPane.ERROR_MESSAGE);
            txtCustomerEmail.requestFocus();
            return false;
        }
        if (txtCustomerAddress.getText().trim().length() == 0) {
            JOptionPane.showMessageDialog(this, "Address is not validated", "ERROR", JOptionPane.ERROR_MESSAGE);
            txtCustomerAddress.requestFocus();
            return false;
        }
        if (!rdoMale.isSelected() && !rdoFeMale.isSelected()) {
            JOptionPane.showMessageDialog(this, "Gender is not validated", "ERROR", JOptionPane.ERROR_MESSAGE);
            return false;
        }
        return true;

    }

//    private boolean isNumberic(String value) {
//        Pattern pattern = Pattern.compile("\\d+");
//        Matcher matcher = pattern.matcher(value);
//        if (!matcher.find()) {
//            return false;
//        } else {
//            return true;
//        }
//    }

    private boolean checkEmail(String value) {
        Pattern pattern = Pattern.compile("[a-zA-Z][_]*\\d*@[a-z]{2,5}[.][a-z]{2,3}");
        Matcher matcher = pattern.matcher(value);
        if (!matcher.find()) {
            return false;
        } else {
            return true;
        }
    }

    private void resetForm() {
        txtCustomerID.setText("");
        txtCustomerName.setText("");
        txtCustomerPhone.setText("");
        txtCustomerEmail.setText("");
        txtCustomerAddress.setText("");
        txtSearchCustomerID.setText("");
        txtSearchCustomerName.setText("");
        rdoMale.setSelected(false);
        rdoFeMale.setSelected(false);
        //cboStatus.setSelectedIndex(0);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(frmCustomer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(frmCustomer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(frmCustomer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(frmCustomer.class.getName()).log(Level.SEVERE, null, ex);
        }
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new frmCustomer().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAddCustomer;
    private javax.swing.JButton btnEdit;
    private javax.swing.JButton btnRefesh;
    private javax.swing.JButton btnSearchRefresh;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JRadioButton rdoFeMale;
    private javax.swing.JRadioButton rdoMale;
    private javax.swing.JTable tblCustomer;
    private javax.swing.JTextField txtCustomerAddress;
    private javax.swing.JTextField txtCustomerEmail;
    private javax.swing.JTextField txtCustomerID;
    private javax.swing.JTextField txtCustomerName;
    private javax.swing.JTextField txtCustomerPhone;
    private javax.swing.JTextField txtSearchCustomerID;
    private javax.swing.JTextField txtSearchCustomerName;
    // End of variables declaration//GEN-END:variables
}
